home *** CD-ROM | disk | FTP | other *** search
- package gratest;
-
- import java.util.Random;
- import java.util.Timer;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
-
- class TetrisCanvas extends Canvas {
- private int iCurrentObject;
- private int iCurrentPosX;
- private int iCurrentPosY;
- private int iCurrentObjectRotation;
- private int iScreenWidth;
- private int iScreenHeight;
- private int iPlayWidth;
- private int iBoxSize;
- private int iNumBoxesWidth = 12;
- private int iNumBoxesHeight;
- private int iWidthPercent = 50;
- private int iPlayHeight;
- private TetElements[] elements;
- private int[][] table;
- protected Timer tetTimer;
- protected Timer dropTimer;
- private Random rand = new Random(System.currentTimeMillis());
- private int iLevelSpeed;
- private int iDropSpeed;
- private int iCurrentSpeed;
- private int iTimerSem = 0;
- private int iScore = 0;
- private int firstIteration = 0;
- private int iTopLine;
- private int iEndGame = 0;
- private int iCurrentLevel = 1;
- Image mainImage;
- Graphics mainGraphics;
-
- public TetrisCanvas(int iNumBoxesParam, int iLevelParam) {
- this.iCurrentLevel = iLevelParam;
- this.iNumBoxesWidth = iNumBoxesParam;
- this.iLevelSpeed = 430 - iLevelParam * 40;
- this.iDropSpeed = 30;
- this.iCurrentSpeed = this.iLevelSpeed;
- this.iScreenWidth = ((Canvas)this).getWidth();
- this.iScreenHeight = ((Canvas)this).getHeight();
- this.iPlayWidth = this.iScreenHeight * this.iWidthPercent / 100;
- this.iBoxSize = this.iPlayWidth / this.iNumBoxesWidth;
- this.iPlayWidth = this.iBoxSize * this.iNumBoxesWidth;
- this.iNumBoxesHeight = this.iScreenHeight / this.iBoxSize;
- this.iTopLine = this.iNumBoxesHeight;
- this.iPlayHeight = this.iNumBoxesHeight * this.iBoxSize;
- this.table = new int[this.iNumBoxesHeight + 1][this.iNumBoxesWidth + 2];
-
- for(int i = 0; i < this.iNumBoxesWidth + 2; ++i) {
- this.table[this.iNumBoxesHeight][i] = 1;
- }
-
- for(int i = 0; i < this.iNumBoxesHeight + 1; ++i) {
- this.table[i][0] = 1;
- this.table[i][this.iNumBoxesWidth + 1] = 1;
- }
-
- this.mainImage = Image.createImage(this.iScreenWidth, this.iScreenHeight);
- this.mainGraphics = this.mainImage.getGraphics();
- this.mainGraphics.setColor(255, 255, 255);
- this.mainGraphics.fillRect(0, 0, this.iScreenWidth, this.iScreenHeight);
- this.mainGraphics.setColor(0, 255, 0);
- this.mainGraphics.fillRect(this.iPlayWidth, 0, 8, this.iScreenHeight);
- this.mainGraphics.fillRect(0, this.iPlayHeight, this.iPlayWidth + 9, 8);
- this.mainGraphics.setColor(0, 0, 0);
- Font font = Font.getFont(32, 1, 8);
- this.mainGraphics.setFont(font);
- this.mainGraphics.drawString("Classic", this.iPlayWidth + 10, 10, 20);
- this.mainGraphics.drawString("Tetris", this.iPlayWidth + 10, 20, 20);
- this.mainGraphics.drawString("Level:", this.iPlayWidth + 10, 30, 20);
- Integer iTemp = new Integer(this.iCurrentLevel);
- this.mainGraphics.drawString(iTemp.toString(), this.iPlayWidth + 10, 40, 20);
- this.mainGraphics.drawString("Score:", this.iPlayWidth + 10, 50, 20);
- this.elements = new TetElements[7];
- this.elements[0] = new TetElements(5, 2);
- this.elements[0].SetPosition(0, 0, 0);
- this.elements[0].SetPosition(0, 1, 5);
- this.elements[0].SetPosition(0, 2, 10);
- this.elements[0].SetPosition(0, 3, 15);
- this.elements[0].SetPosition(0, 4, 20);
- this.elements[0].SetBottomConstraintValues(0, 1, -1, 0, 0, 0, 0);
- this.elements[0].SetLeftConstraintValues(0, 5, -1, -2, -3, -4, -5);
- this.elements[0].SetRightConstraintValues(0, 5, 1, 6, 11, 16, 21);
- this.elements[0].SetRotMove(0, -2, 2);
- this.elements[0].SetRotConstraintValues(0, 0, 4, -11, -12, 11, 12, 0);
- this.elements[0].SetPosition(1, 0, 0);
- this.elements[0].SetPosition(1, 1, 1);
- this.elements[0].SetPosition(1, 2, 2);
- this.elements[0].SetPosition(1, 3, 3);
- this.elements[0].SetPosition(1, 4, 4);
- this.elements[0].SetBottomConstraintValues(1, 5, 5, 6, 7, 8, 9);
- this.elements[0].SetLeftConstraintValues(1, 1, -1, 0, 0, 0, 0);
- this.elements[0].SetRightConstraintValues(1, 1, -1, 0, 0, 0, 0);
- this.elements[0].SetRotMove(1, 2, -2);
- this.elements[0].SetRotConstraintValues(1, 0, 4, 7, 12, -111, -112, 0);
- this.elements[1] = new TetElements(4, 2);
- this.elements[1].SetPosition(0, 0, 1);
- this.elements[1].SetPosition(0, 1, 2);
- this.elements[1].SetPosition(0, 2, 5);
- this.elements[1].SetPosition(0, 3, 6);
- this.elements[1].SetBottomConstraintValues(0, 3, 7, 10, 11, 0, 0);
- this.elements[1].SetLeftConstraintValues(0, 2, 0, -2, 0, 0, 0);
- this.elements[1].SetRightConstraintValues(0, 2, 3, 7, 0, 0, 0);
- this.elements[1].SetRotConstraintValues(0, 0, 2, 0, 11, 0, 0, 0);
- this.elements[1].SetPosition(1, 0, 0);
- this.elements[1].SetPosition(1, 1, 5);
- this.elements[1].SetPosition(1, 2, 6);
- this.elements[1].SetPosition(1, 3, 11);
- this.elements[1].SetBottomConstraintValues(1, 2, 10, 16, 0, 0, 0);
- this.elements[1].SetLeftConstraintValues(1, 3, -1, -2, 10, 0, 0);
- this.elements[1].SetRightConstraintValues(1, 3, 1, 7, 12, 0, 0);
- this.elements[1].SetRotConstraintValues(1, 0, 2, 1, 2, 0, 0, 0);
- this.elements[2] = new TetElements(4, 1);
- this.elements[2].SetPosition(0, 0, 0);
- this.elements[2].SetPosition(0, 1, 1);
- this.elements[2].SetPosition(0, 2, 5);
- this.elements[2].SetPosition(0, 3, 6);
- this.elements[2].SetBottomConstraintValues(0, 2, 10, 11, 0, 0, 0);
- this.elements[2].SetLeftConstraintValues(0, 2, -1, -2, 0, 0, 0);
- this.elements[2].SetRightConstraintValues(0, 2, 2, 7, 0, 0, 0);
- this.elements[3] = new TetElements(4, 4);
- this.elements[3].SetPosition(0, 0, 0);
- this.elements[3].SetPosition(0, 1, 1);
- this.elements[3].SetPosition(0, 2, 2);
- this.elements[3].SetPosition(0, 3, 6);
- this.elements[3].SetBottomConstraintValues(0, 3, 5, 7, 11, 0, 0);
- this.elements[3].SetLeftConstraintValues(0, 2, -1, 5, 0, 0, 0);
- this.elements[3].SetRightConstraintValues(0, 2, 3, 6, 0, 0, 0);
- this.elements[3].SetRotConstraintValues(0, 0, 2, 5, 10, 0, 0, 0);
- this.elements[3].SetPosition(1, 0, 0);
- this.elements[3].SetPosition(1, 1, 5);
- this.elements[3].SetPosition(1, 2, 6);
- this.elements[3].SetPosition(1, 3, 10);
- this.elements[3].SetBottomConstraintValues(1, 2, 11, 15, 0, 0, 0);
- this.elements[3].SetLeftConstraintValues(1, 3, -1, -2, -3, 0, 0);
- this.elements[3].SetRightConstraintValues(1, 3, 1, 7, 11, 0, 0);
- this.elements[3].SetRotConstraintValues(1, 0, 2, 1, 7, 0, 0, 0);
- this.elements[3].SetPosition(2, 0, 1);
- this.elements[3].SetPosition(2, 1, 5);
- this.elements[3].SetPosition(2, 2, 6);
- this.elements[3].SetPosition(2, 3, 7);
- this.elements[3].SetBottomConstraintValues(2, 3, 10, 11, 12, 0, 0);
- this.elements[3].SetLeftConstraintValues(2, 2, 0, -2, 0, 0, 0);
- this.elements[3].SetRightConstraintValues(2, 2, 2, 8, 0, 0, 0);
- this.elements[3].SetRotConstraintValues(2, 0, 1, 11, 0, 0, 0, 0);
- this.elements[3].SetPosition(3, 0, 1);
- this.elements[3].SetPosition(3, 1, 5);
- this.elements[3].SetPosition(3, 2, 6);
- this.elements[3].SetPosition(3, 3, 11);
- this.elements[3].SetBottomConstraintValues(3, 2, 10, 16, 0, 0, 0);
- this.elements[3].SetLeftConstraintValues(3, 3, 0, -2, 10, 0, 0);
- this.elements[3].SetRightConstraintValues(3, 3, 2, 7, 12, 0, 0);
- this.elements[3].SetRotConstraintValues(3, 0, 2, 0, 2, 0, 0, 0);
- this.elements[4] = new TetElements(4, 4);
- this.elements[4].SetPosition(0, 0, 2);
- this.elements[4].SetPosition(0, 1, 5);
- this.elements[4].SetPosition(0, 2, 6);
- this.elements[4].SetPosition(0, 3, 7);
- this.elements[4].SetBottomConstraintValues(0, 3, 10, 11, 12, 0, 0);
- this.elements[4].SetLeftConstraintValues(0, 2, 1, -2, 0, 0, 0);
- this.elements[4].SetRightConstraintValues(0, 2, 3, 8, 0, 0, 0);
- this.elements[4].SetRotConstraintValues(0, 0, 3, 0, 10, 11, 0, 0);
- this.elements[4].SetPosition(1, 0, 0);
- this.elements[4].SetPosition(1, 1, 5);
- this.elements[4].SetPosition(1, 2, 10);
- this.elements[4].SetPosition(1, 3, 11);
- this.elements[4].SetBottomConstraintValues(1, 2, 15, 16, 0, 0, 0);
- this.elements[4].SetLeftConstraintValues(1, 3, -1, -2, -3, 0, 0);
- this.elements[4].SetRightConstraintValues(1, 3, 1, 6, 12, 0, 0);
- this.elements[4].SetRotConstraintValues(1, 0, 2, 1, 2, 0, 0, 0);
- this.elements[4].SetPosition(2, 0, 0);
- this.elements[4].SetPosition(2, 1, 1);
- this.elements[4].SetPosition(2, 2, 2);
- this.elements[4].SetPosition(2, 3, 5);
- this.elements[4].SetBottomConstraintValues(2, 3, 6, 7, 10, 0, 0);
- this.elements[4].SetLeftConstraintValues(2, 2, -1, -2, 0, 0, 0);
- this.elements[4].SetRightConstraintValues(2, 2, 3, 6, 0, 0, 0);
- this.elements[4].SetRotConstraintValues(2, 0, 2, 6, 11, 0, 0, 0);
- this.elements[4].SetPosition(3, 0, 0);
- this.elements[4].SetPosition(3, 1, 1);
- this.elements[4].SetPosition(3, 2, 6);
- this.elements[4].SetPosition(3, 3, 11);
- this.elements[4].SetBottomConstraintValues(3, 2, 5, 16, 0, 0, 0);
- this.elements[4].SetLeftConstraintValues(3, 3, -1, 5, 10, 0, 0);
- this.elements[4].SetRightConstraintValues(3, 3, 2, 7, 12, 0, 0);
- this.elements[4].SetRotConstraintValues(3, 0, 3, 5, 7, 2, 0, 0);
- this.elements[5] = new TetElements(4, 4);
- this.elements[5].SetPosition(0, 0, 0);
- this.elements[5].SetPosition(0, 1, 5);
- this.elements[5].SetPosition(0, 2, 6);
- this.elements[5].SetPosition(0, 3, 7);
- this.elements[5].SetBottomConstraintValues(0, 3, 10, 11, 12, 0, 0);
- this.elements[5].SetLeftConstraintValues(0, 2, -1, -2, 0, 0, 0);
- this.elements[5].SetRightConstraintValues(0, 2, 1, 8, 0, 0, 0);
- this.elements[5].SetRotConstraintValues(0, 0, 2, 1, 10, 0, 0, 0);
- this.elements[5].SetPosition(1, 0, 0);
- this.elements[5].SetPosition(1, 1, 1);
- this.elements[5].SetPosition(1, 2, 5);
- this.elements[5].SetPosition(1, 3, 10);
- this.elements[5].SetBottomConstraintValues(1, 2, 15, 6, 0, 0, 0);
- this.elements[5].SetLeftConstraintValues(1, 3, -1, -2, -3, 0, 0);
- this.elements[5].SetRightConstraintValues(1, 3, 2, 6, 11, 0, 0);
- this.elements[5].SetRotConstraintValues(1, 0, 2, 2, 7, 0, 0, 0);
- this.elements[5].SetPosition(2, 0, 0);
- this.elements[5].SetPosition(2, 1, 1);
- this.elements[5].SetPosition(2, 2, 2);
- this.elements[5].SetPosition(2, 3, 7);
- this.elements[5].SetBottomConstraintValues(2, 3, 5, 6, 12, 0, 0);
- this.elements[5].SetLeftConstraintValues(2, 2, -1, 6, 0, 0, 0);
- this.elements[5].SetRightConstraintValues(2, 2, 3, 8, 0, 0, 0);
- this.elements[5].SetRotConstraintValues(2, 0, 3, 6, 10, 11, 0, 0);
- this.elements[5].SetPosition(3, 0, 1);
- this.elements[5].SetPosition(3, 1, 6);
- this.elements[5].SetPosition(3, 2, 10);
- this.elements[5].SetPosition(3, 3, 11);
- this.elements[5].SetBottomConstraintValues(3, 2, 15, 16, 0, 0, 0);
- this.elements[5].SetLeftConstraintValues(3, 3, 0, 5, -2, 0, 0);
- this.elements[5].SetRightConstraintValues(3, 3, 2, 7, 11, 0, 0);
- this.elements[5].SetRotConstraintValues(3, 0, 3, 0, 5, 7, 0, 0);
- this.elements[6] = new TetElements(4, 2);
- this.elements[6].SetPosition(0, 0, 0);
- this.elements[6].SetPosition(0, 1, 1);
- this.elements[6].SetPosition(0, 2, 6);
- this.elements[6].SetPosition(0, 3, 7);
- this.elements[6].SetBottomConstraintValues(0, 3, 5, 11, 12, 0, 0);
- this.elements[6].SetLeftConstraintValues(0, 2, -1, 5, 0, 0, 0);
- this.elements[6].SetRightConstraintValues(0, 2, 2, 8, 0, 0, 0);
- this.elements[6].SetRotConstraintValues(0, 0, 2, 5, 10, 0, 0, 0);
- this.elements[6].SetPosition(1, 0, 1);
- this.elements[6].SetPosition(1, 1, 5);
- this.elements[6].SetPosition(1, 2, 6);
- this.elements[6].SetPosition(1, 3, 10);
- this.elements[6].SetBottomConstraintValues(1, 2, 11, 15, 0, 0, 0);
- this.elements[6].SetLeftConstraintValues(1, 3, 0, -2, -3, 0, 0);
- this.elements[6].SetRightConstraintValues(1, 3, 2, 7, 11, 0, 0);
- this.elements[6].SetRotConstraintValues(1, 0, 2, 0, 7, 0, 0, 0);
- this.iCurrentObject = 1;
- this.iCurrentPosX = this.iNumBoxesWidth / 2;
- this.iCurrentPosY = 0;
- this.iCurrentObjectRotation = 0;
-
- int iRand;
- for(iRand = -1; iRand < 0; iRand = this.rand.nextInt()) {
- }
-
- this.iCurrentObject = iRand % 7;
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
- this.tetTimer = new Timer();
- this.tetTimer.schedule(new TetrisTask(this), 0L, (long)this.iLevelSpeed);
- }
-
- public void drawElement(int element, int elementrot, int x, int y, int r, int g, int b, Graphics gr) {
- if (r == 255 && g == 255 && b == 255) {
- gr.setColor(r, g, b);
- } else if (this.iCurrentObject == 0) {
- gr.setColor(0, 0, 0);
- } else if (this.iCurrentObject == 1) {
- gr.setColor(255, 0, 255);
- } else if (this.iCurrentObject == 2) {
- gr.setColor(0, 0, 255);
- } else if (this.iCurrentObject == 3) {
- gr.setColor(0, 255, 255);
- } else if (this.iCurrentObject == 4) {
- gr.setColor(0, 166, 88);
- } else if (this.iCurrentObject == 5) {
- gr.setColor(166, 88, 255);
- } else if (this.iCurrentObject == 6) {
- gr.setColor(166, 88, 88);
- }
-
- TetElements Tet = this.elements[element];
- int iNumBlocs = Tet.iNumBlocs;
-
- for(int i = 0; i < iNumBlocs; ++i) {
- int value = Tet.GetPosition(elementrot, i);
- int xrect = value % 5;
- int yrect = value / 5;
- int xpos = x * this.iBoxSize + xrect * this.iBoxSize;
- int ypos = y * this.iBoxSize + yrect * this.iBoxSize;
- gr.fillRect(xpos, ypos, this.iBoxSize - 1, this.iBoxSize - 1);
- }
-
- }
-
- public void cutLine(int iLine) {
- Image imagetemp = Image.createImage(this.iScreenWidth, this.iScreenHeight);
- Graphics gtemp = imagetemp.getGraphics();
- gtemp.drawImage(this.mainImage, 0, 0, 20);
- this.mainGraphics.setClip(0, 0, this.iPlayWidth, iLine * this.iBoxSize);
- this.mainGraphics.setColor(255, 255, 255);
- this.mainGraphics.fillRect(0, 0, this.iScreenWidth, this.iBoxSize);
- this.mainGraphics.drawImage(imagetemp, 0, this.iBoxSize, 20);
- this.mainGraphics.setClip(0, 0, this.iScreenWidth, this.iScreenHeight);
- }
-
- public int MoveTable(int iLine) {
- for(int i = iLine - 1; i > -1; --i) {
- for(int j = 1; j < this.iNumBoxesWidth + 1; ++j) {
- this.table[i + 1][j] = this.table[i][j];
- }
- }
-
- return 0;
- }
-
- public int checkFullLine(int iBottomLine, int iNumLinesToCheck) {
- int iEmpty = 0;
- int iInd = 0;
-
- for(int i = 0; i < iNumLinesToCheck; ++i) {
- if (iInd == 1 && iEmpty == 0) {
- return i - 1;
- }
-
- iEmpty = 0;
-
- for(int j = 1; j < this.iNumBoxesWidth + 1; ++j) {
- iInd = 1;
- if (iBottomLine - i < 0) {
- return -1;
- }
-
- if (this.table[iBottomLine - i][j] == 0) {
- iEmpty = 1;
- }
- }
- }
-
- return -1;
- }
-
- public int CalculateNext() {
- int iGo = 1;
- TetElements Tet = this.elements[this.iCurrentObject];
- int iNumConstraints = Tet.GetBottomConstraintValue(this.iCurrentObjectRotation, 0);
-
- for(int j = 0; j < Tet.iNumBlocs; ++j) {
- int value = Tet.GetPosition(this.iCurrentObjectRotation, j);
- int xrect = value % 5;
- int yrect = value / 5;
- int xpos = this.iCurrentPosX + 1 + xrect;
- int ypos1 = this.iCurrentPosY + yrect;
- if (this.table[ypos1][xpos] == 1) {
- this.mainGraphics.setColor(0, 0, 0);
- this.mainGraphics.drawString("Game Over", 10, 50, 20);
- this.iEndGame = 1;
- this.tetTimer.cancel();
- }
- }
-
- for(int i = 0; i < iNumConstraints; ++i) {
- int iValue = Tet.GetBottomConstraintValue(this.iCurrentObjectRotation, i + 1);
- if (iValue < 0) {
- if (this.table[this.iCurrentPosY + 5][this.iCurrentPosX - iValue] == 1) {
- iGo = 0;
- }
- } else {
- int x = iValue % 5;
- int y = iValue / 5;
- if (this.table[this.iCurrentPosY + y][this.iCurrentPosX + 1 + x] == 1) {
- iGo = 0;
- }
- }
- }
-
- if (iGo == 1) {
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 255, 255, 255, this.mainGraphics);
- ++this.iCurrentPosY;
- ++this.firstIteration;
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
- }
-
- if (iGo == 0) {
- this.firstIteration = 0;
- int ypos = 0;
-
- for(int j = 0; j < Tet.iNumBlocs; ++j) {
- int value = Tet.GetPosition(this.iCurrentObjectRotation, j);
- int xrect = value % 5;
- int yrect = value / 5;
- int xpos = this.iCurrentPosX + 1 + xrect;
- ypos = this.iCurrentPosY + yrect;
- this.table[ypos][xpos] = 1;
- if (ypos < this.iTopLine) {
- this.iTopLine = ypos;
- }
- }
-
- int iEnd = 6;
- int iLineScore = 0;
-
- while(iEnd > 0) {
- int iRes = this.checkFullLine(ypos, iEnd);
- if (iRes > -1) {
- ypos -= iRes;
- this.cutLine(ypos + 1);
- this.MoveTable(ypos);
- iEnd = iEnd - 1 - iRes;
- ++iLineScore;
- ++this.iTopLine;
- } else {
- --ypos;
- --iEnd;
- }
- }
-
- if (iLineScore < 3) {
- this.iScore += iLineScore * 50;
- } else {
- this.iScore += iLineScore * 200;
- }
-
- if (iLineScore == 0) {
- this.iScore += 4;
- }
-
- if (this.iScore > this.iCurrentLevel * 1000) {
- if (this.iLevelSpeed > 100) {
- this.iLevelSpeed -= 40;
- }
-
- ++this.iCurrentLevel;
- Integer iTemp1 = new Integer(this.iCurrentLevel);
- this.mainGraphics.setColor(255, 255, 255);
- this.mainGraphics.fillRect(this.iPlayWidth + 10, 40, 50, 10);
- this.mainGraphics.setColor(0, 0, 0);
- this.mainGraphics.drawString(iTemp1.toString(), this.iPlayWidth + 10, 40, 20);
- }
-
- int iRand;
- for(iRand = -1; iRand < 0; iRand = this.rand.nextInt()) {
- }
-
- this.iCurrentObject = iRand % 7;
- if (this.iCurrentObject < 0) {
- this.iCurrentObject = 1;
- }
-
- this.iCurrentObjectRotation = 0;
- this.iCurrentPosX = 6;
- this.iCurrentPosY = 0;
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
- Integer iTempScore = new Integer(this.iScore);
- this.mainGraphics.setColor(255, 255, 255);
- this.mainGraphics.fillRect(this.iPlayWidth + 10, 60, 50, 10);
- this.mainGraphics.setColor(0, 0, 0);
- this.mainGraphics.drawString(iTempScore.toString(), this.iPlayWidth + 10, 60, 20);
- if (this.iCurrentSpeed == this.iDropSpeed) {
- this.iCurrentSpeed = this.iLevelSpeed;
- this.dropTimer.cancel();
- this.tetTimer = new Timer();
- this.tetTimer.schedule(new TetrisTask(this), 0L, (long)this.iCurrentSpeed);
- this.iTimerSem = 0;
- }
- }
-
- return 1;
- }
-
- public int CalculateLeft() {
- int iGo = 1;
- TetElements Tet = this.elements[this.iCurrentObject];
- int iNumConstraints = Tet.GetLeftConstraintValue(this.iCurrentObjectRotation, 0);
-
- for(int i = 0; i < iNumConstraints; ++i) {
- int iValue = Tet.GetLeftConstraintValue(this.iCurrentObjectRotation, i + 1);
- if (iValue < 0) {
- if (this.table[this.iCurrentPosY - iValue - 1][this.iCurrentPosX] == 1) {
- iGo = 0;
- break;
- }
- } else {
- int x = iValue % 5;
- int y = iValue / 5;
- if (this.table[this.iCurrentPosY + y][this.iCurrentPosX + 1 + x] == 1) {
- iGo = 0;
- break;
- }
- }
- }
-
- if (iGo == 1) {
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 255, 255, 255, this.mainGraphics);
- --this.iCurrentPosX;
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
- }
-
- return 1;
- }
-
- public int CalculateRight() {
- int iGo = 1;
- TetElements Tet = this.elements[this.iCurrentObject];
- int iNumConstraints = Tet.GetRightConstraintValue(this.iCurrentObjectRotation, 0);
-
- for(int i = 0; i < iNumConstraints; ++i) {
- int iValue = Tet.GetRightConstraintValue(this.iCurrentObjectRotation, i + 1);
- if (iValue < 0) {
- if (this.table[this.iCurrentPosY - iValue - 1][this.iCurrentPosX + 6] == 1) {
- iGo = 0;
- break;
- }
- } else {
- int x = iValue % 5;
- int y = iValue / 5;
- if (this.table[this.iCurrentPosY + y][this.iCurrentPosX + 1 + x] == 1) {
- iGo = 0;
- break;
- }
- }
- }
-
- if (iGo == 1) {
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 255, 255, 255, this.mainGraphics);
- ++this.iCurrentPosX;
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
- }
-
- return 1;
- }
-
- public int CalculateRotate() {
- int iGo = 1;
- TetElements Tet = this.elements[this.iCurrentObject];
- if (Tet.iNoRotConstraints == 1) {
- return iGo;
- } else {
- int iNumConstraints = Tet.GetRotConstraintValue(this.iCurrentObjectRotation, 0);
-
- for(int i = 0; i < iNumConstraints; ++i) {
- int iValue = Tet.GetRotConstraintValue(this.iCurrentObjectRotation, i + 1);
- if (iValue < 0) {
- int iYChanges = 0;
- iValue = 0 - iValue;
- if (iValue > 100) {
- iYChanges = 1;
- iValue -= 100;
- }
-
- int x = iValue % 5;
- int y = iValue / 5;
- int xpos;
- int ypos;
- if (iYChanges == 0) {
- ypos = this.iCurrentPosY + y;
- xpos = this.iCurrentPosX + 1 - x;
- } else {
- ypos = this.iCurrentPosY - y;
- xpos = this.iCurrentPosX + 1 + x;
- }
-
- if (this.table[ypos][xpos] == 1) {
- iGo = 0;
- break;
- }
- } else {
- int x = iValue % 5;
- int y = iValue / 5;
- if (this.table[this.iCurrentPosY + y][this.iCurrentPosX + 1 + x] == 1) {
- iGo = 0;
- break;
- }
- }
- }
-
- return iGo;
- }
- }
-
- public void Rotate() {
- int iCanRotate = this.CalculateRotate();
- if (iCanRotate != 0) {
- TetElements Tet = this.elements[this.iCurrentObject];
- int iNumRotations = Tet.iNumRotations;
- int iMoveX = Tet.rotMove[this.iCurrentObjectRotation][0];
- int iMoveY = Tet.rotMove[this.iCurrentObjectRotation][1];
- if (iMoveX == -10) {
- iMoveX = 0;
- iMoveY = 0;
- }
-
- if (iNumRotations > 1) {
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 255, 255, 255, this.mainGraphics);
- ++this.iCurrentObjectRotation;
- if (this.iCurrentObjectRotation > iNumRotations - 1) {
- this.iCurrentObjectRotation = 0;
- }
-
- this.iCurrentPosX += iMoveX;
- this.iCurrentPosY += iMoveY;
- this.drawElement(this.iCurrentObject, this.iCurrentObjectRotation, this.iCurrentPosX, this.iCurrentPosY, 0, 255, 255, this.mainGraphics);
- }
-
- }
- }
-
- public void paint(Graphics g) {
- g.drawImage(this.mainImage, 0, 0, 20);
- }
-
- public void keyPressed(int keyCode) {
- switch (((Canvas)this).getGameAction(keyCode)) {
- case 1:
- this.Rotate();
- break;
- case 2:
- this.CalculateLeft();
- case 3:
- case 4:
- default:
- break;
- case 5:
- this.CalculateRight();
- break;
- case 6:
- this.iCurrentSpeed = this.iDropSpeed;
- if (this.iTimerSem == 0 && this.iEndGame == 0) {
- this.tetTimer.cancel();
- this.dropTimer = new Timer();
- this.dropTimer.schedule(new TetrisTask(this), 0L, (long)this.iDropSpeed);
- ++this.iTimerSem;
- }
- }
-
- }
- }
-